package com.megaclient.activity;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;

import com.megaclient.R;
import com.utility.MousePress;
import com.utility.Point;

public class ControlActivity extends Activity {
	private boolean leftMouseHold;
	private int xMouse;
	private int yMouse;
	private TextView touchTextView;
	private Button leftButton;
	private Button rightButton;
	private String host;
	private int port;

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.control);
		
		// Get extras.
		host = getIntent().getExtras().getString("host");
		System.out.println(host);
		port = getIntent().getExtras().getInt("port");
		System.out.println(port);

		// Init touch view.
		touchTextView = (TextView) findViewById(R.id.touchTextView);
		touchTextView.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(final View v, final MotionEvent event) {
				touchTouchPad(event);
				return true;
			}
		});
		
		// Init left button.
		leftButton = (Button)findViewById(R.id.leftButton);
		leftButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(final View v) {				
				try {
					final Socket socket = new Socket(host, port);
					sendLeftPress(socket);
				} catch (final UnknownHostException e) {
					e.printStackTrace();
				} catch (final IOException e) {
					e.printStackTrace();
				}
			}
		});
		
		// Init right button.
		rightButton = (Button)findViewById(R.id.rightButton);
		rightButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(final View v) {				
				try {
					final Socket socket = new Socket(host, port);
					sendRightPress(socket);
				} catch (final UnknownHostException e) {
					e.printStackTrace();
				} catch (final IOException e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void touchTouchPad(final MotionEvent event) {
		final int x = (int) event.getX();
		final int y = (int) event.getY();
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			leftMouseHold = true;
			xMouse = x;
			yMouse = y;
			break;

		case MotionEvent.ACTION_MOVE:
			if (leftMouseHold) {
				final int dx = x - xMouse;
				final int dy = y - yMouse;
				xMouse += dx;
				yMouse += dy;

				try {
					final Socket socket = new Socket(host, port);
					sendDelta(socket, dx, dy);
				} catch (final NumberFormatException e) {
					e.printStackTrace();
				} catch (final UnknownHostException e) {
					e.printStackTrace();
				} catch (final IOException e) {
					e.printStackTrace();
				}
			}
			break;

		case MotionEvent.ACTION_UP:
			leftMouseHold = false;
			break;
		}
	}

	public void sendDelta(final Socket socket, final int dx, final int dy) {
		try {
			final ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
			final Point point = new Point(dx, dy);
			oos.writeObject(point);

			oos.close();
			socket.close();
		} catch (final StreamCorruptedException e) {
			e.printStackTrace();
		} catch (final IOException e) {
			e.printStackTrace();
		}
	}
	
	public void sendLeftPress(final Socket socket) {
		System.out.println("Left press in ControlActivity");
		sendPress(socket, MousePress.LEFT_PRESS);
	}
	
	public void sendRightPress(final Socket socket) {
		System.out.println("Right press in ControlActivity");
		sendPress(socket, MousePress.RIGHT_PRESS);
	}
	
	private void sendPress(final Socket socket, final int mousePressType) {
		try {
			System.out.println("Wrote press1");
			final ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
			final MousePress press = new MousePress(mousePressType);
			oos.writeObject(press);
			System.out.println("Wrote press2");

			oos.close();
			socket.close();
		} catch (final StreamCorruptedException e) {
			e.printStackTrace();
		} catch (final IOException e) {
			e.printStackTrace();
		}
	}
}
